package com.hmall.user.intercept;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqConfig {
    @Bean
    public TopicExchange topicExchange(){
            return new TopicExchange(MqConstants.HMALL_ORDER_EXCHANGE, true, false);
    }


    @Bean
    public Queue insertQueue(){
        int time = 1000*60*30;
        return QueueBuilder.durable(MqConstants.HMALL_ORDER_INSERT_QUEUE) // 指定队列名称，并持久化
                .ttl(time) // 设置队列的超时时间
                .deadLetterExchange(MqConstants.HMALL_TIME_OUT_ORDER_EXCHANGE) // 指定死信交换机
                .build();
    }


    @Bean
    public Binding insertQueueBinding(){
        return BindingBuilder.bind(insertQueue()).to(topicExchange()).with(MqConstants.HMALL_CREATE_ORDER_KEY);
    }

    // 声明死信交换机 dl.direct
    @Bean
    public DirectExchange timeoutExchange(){
        return new DirectExchange(MqConstants.HMALL_TIME_OUT_ORDER_EXCHANGE, true, false);
    }

    // 声明存储死信的队列 dl.queue
    @Bean
    public Queue timeoutQueue(){
        return new Queue(MqConstants.HMALL_TIME_OUT_ORDER_QUEUE, true);
    }

    // 将死信队列 与 死信交换机绑定
    @Bean
    public Binding timeoutBinding(){
        return BindingBuilder.bind(timeoutQueue()).to(timeoutExchange()).with(MqConstants.HMALL_TIME_OUT_ORDER_KEY);
    }
}